package com.yxh.service.impl;

import com.yxh.service.RedisDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * Created by 32988 on 2018/5/31.
 */
@Component
    public class RedisDataSourceImpl implements RedisDataSource {

        @Autowired
        private ShardedJedisPool shardedJedisPool;



        public ShardedJedis getRedisClient() {
            try {
                ShardedJedis shardJedis = shardedJedisPool.getResource();
                return shardJedis;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public void returnResource(ShardedJedis shardedJedis) {
            shardedJedisPool.returnResource(shardedJedis);
        }

        public void returnResource(ShardedJedis shardedJedis, boolean broken) {
            if (broken) {
                shardedJedisPool.returnBrokenResource(shardedJedis);
            } else {
                shardedJedisPool.returnResource(shardedJedis);
            }
        }
    }
